package com.rocket.demo2;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

/**
 * 顺序模式
 */
public class Puducer2 {
    static String[] msgs = {
            "15103111039,创建",
            "15103111065,创建",
            "15103111039,付款",
            "15103117235,创建",
            "15103111065,付款",
            "15103117235,付款",
            "15103111065,完成",
            "15103111039,推送",
            "15103117235,完成",
            "15103111039,完成"
    };

    public static void main(String[] args) throws Exception {
        DefaultMQProducer mqProducer = new DefaultMQProducer("puducer2");
        mqProducer.setNamesrvAddr("192.168.126.137:9876");
        mqProducer.start();
        for (String msg : msgs){
            Message message = new Message("Tipic2", "taga", msg.getBytes());
            Long orderId = Long.parseLong(msg.split(",")[0]);
            mqProducer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    Long orderId = (Long) o;
                    long index = orderId % list.size();
                    System.out.println(list.get((int)index));
                    return list.get((int)index);
                }
            }, orderId);
        }
    }
}
